/*******************************发送分页查询ajax请求******************************/
var currentPage = 1; //当前页
var pageSize = 10; //每页显示条数
var totalCount = 0; //总记录条数
var totalPage = 0; //总页数

function PageFind() {
    //1.获取输入框上的值
    //2.发送ajax
    $.ajax({
        url: "menu/findByPage",
        type: "post",
        dataType: "JSON",
        data: {
            currentPage: currentPage,
            pageSize: pageSize
        },
        success: function(pageBean) {
            //设置参数
            totalCount = pageBean.totalCount;
            totalPage = parseInt(pageBean.totalPage);
            //数据插入函数
            showTableData(pageBean.list);
        }
    })
}

//发送ajax
PageFind();

function showTableData(dataList) {
    //1.设置当前页和总页数
    var pageNumberData = currentPage + '/' + totalPage;
    $('#pageNumber').html(pageNumberData);
    //2.设置表格数据
    //2.1清空表格
    $('#tableData').html('');
    //2.2遍历数据
    var trs = '';
    for (var i = 0; i < dataList.length; i++) {
        var dataDetail = dataList[i];
        //判断修改数据
        //改日期
        var cDate = new Date(dataDetail.menuCtime).format('yyyy-MM-dd hh:mm:ss');
        var tr = `
               <tr>
                    <td>${dataDetail.menuId}</td>
                    <td>${dataDetail.menuName}</td>
                    <td>${dataDetail.menuUrl}</td>
                    <td hidden="hidden">${dataDetail.pmenuId}</td>
                    <td>${dataDetail.menuIcon}</td>
                    <td>${cDate}</td>
                    <td>
                        <button class="btn btn-mini " type="button" id="updateBtn">编辑</button>
                        <button class="btn btn-mini " type="button" id="deleteBtn">删除</button>
                    </td>
                </tr>`;
        //添加数据
        trs += tr;
    }
    //2.3将数据加入
    $('#tableData').html(trs);
    //4.更新事件
    $('[id = updateBtn]').click(function() {
        //设置账号
        $('#updateMenuId').val($(this).parent().parent().find('td').eq(0).html());
        $('#updateMenuName').val($(this).parent().parent().find('td').eq(1).html());
        $('#updateMenuUrl').val($(this).parent().parent().find('td').eq(2).html());
        $('#updatePmenuId').val($(this).parent().parent().find('td').eq(3).html());
        $('#updatePic').val($(this).parent().parent().find('td').eq(4).html());
        //显示弹窗
        showPanel('#update');
    });
    //4.更新事件
    $('[id = deleteBtn]').click(function() {
        //设置账号
        var menuId = $(this).parent().parent().find('td').eq(0).html();
        //显示弹窗
        layer.confirm('是否删除', {
            btn: ['确定','取消'] //按钮
        }, function(){
            deleteAjax(menuId);
        }, function(){
            layer.msg('已经取消', {icon: 1});
        });
    });
}
//上一页 下一页按钮 搜索 重置 触发事件
$('#prePageBtn').click(function() {
    currentPage -= 1;
    if (currentPage < 1) {
        currentPage = 1;
        return;
    }
    PageFind();
});
$('#nextPageBtn').click(function() {
    currentPage += 1;
    if (currentPage > totalPage) {
        currentPage = totalPage;
        return;
    }
    PageFind();
});
/* *****************增加菜单事件********************/
$('#addMune').click(function () {
    $('#addMenuId').val('');
    $('#addMenuName').val('');
    $('#addMenuUrl').val('');
    $('#addPmenuId').val(1);
    $('#addPic').val('');
    //弹出窗口
    showPanel('#add');

});
function addAjax() {
    //1.获取参数
   var menuId=  $('#addMenuId').val();
   var menuName=  $('#addMenuName').val();
   var menuUrl= $('#addMenuUrl').val();
   var pmenuId= $('#addPmenuId').val();
   var menuIcon= $('#addPic').val();
   //2.发送ajax
    $.ajax({
        url: "menu/addMenu",
        type: "post",
        dataType: "JSON",
        contentType:"application/json;charset=UTF-8",
        data: JSON.stringify({
            menuId: menuId,
            menuName: menuName,
            menuUrl: menuUrl,
            pmenuId: pmenuId,
            menuIcon: menuIcon
        }),
        success: function(data) {
            if(data.id == 1){
                layer.confirm(data.message, {
                    btn: ['确定'] //按钮
                });
                PageFind();
                hide('#add');
            }else {
                layer.confirm(data.message, {
                    btn: ['确定'] //按钮
                });
            }
        }
    });
};
/* **********************发送修改密码AJAX*********************** */
function updateAjax() {
    //1.获取参数
    var menuId=  $('#updateMenuId').val();
    var menuName=  $('#updateMenuName').val();
    var menuUrl= $('#updateMenuUrl').val();
    var pmenuId= $('#updatePmenuId').val();
    var menuIcon= $('#updatePic').val();
    //2.判断
    if (menuName == '' || menuUrl == '' || menuIcon == '') {
        layer.confirm('能为空，请重新输入', {
            btn: ['确定'] //按钮
        });
        return;
    }
    //3.发送AJAX
    $.ajax({
        url: "menu/updateMenu",
        type: "post",
        dataType: "JSON",
        contentType:"application/json;charset=UTF-8",
        data: JSON.stringify({
            menuId: menuId,
            menuName: menuName,
            menuUrl: menuUrl,
            pmenuId: pmenuId,
            menuIcon: menuIcon
        }),
        success: function(data) {
            if (data.id == 1) {
                //修改成功
                layer.confirm(data.message, {
                    btn: ['确定'] //按钮
                });
                hide('#update');
                PageFind();
            } else if (data.id == 0) {
                //修改失败
                layer.confirm(data.message, {
                    btn: ['确定'] //按钮
                });
            }
        }
    })
};
/**********删除*****/
function deleteAjax(menuId) {
    $.ajax({
        url: "menu/deleteMenu?menuId="+menuId,
        type: "post",
        dataType: "JSON",
        contentType:"application/json;charset=UTF-8",
        data: {},
        success: function(data) {
            if (data.id == 1) {
                //删除成功
                layer.msg(data.message, {icon: 1});
                PageFind();
            } else if (data.id == 0) {
                layer.msg(data.message, {icon: 1});
            }
        }
    });
}